package script
{
	import eventUtil.EventCenter;
	
	import laya.components.Script;
	import laya.display.Sprite;
	import laya.ui.View;
	import laya.utils.Browser;
	
	import model.lesson.EnglishBookPage;
	import model.quiz.MathQuizResultController;
	
	import script.baseView.BaseViewController;
	
	import ui.MainPagePanelUI;
	import ui.PopUpDialogUI;
	import ui.learn.BookPagePanelUI;
	import ui.learn.ChineseBookContentPanelUI;
	import ui.learn.ChooseLevelPanelUI;
	import ui.learn.ChooseReciteDatePanelUI;
	import ui.learn.DictationPanelUI;
	import ui.learn.EnglishBookContentPanelUI;
	import ui.learn.EnglishWordAddPanelUI;
	import ui.learn.LearnWordsPanelUI;
	import ui.login.LoginPanelUI;
	import ui.login.RegisterPanelUI;
	import ui.prefab.TopMenuUI;
	import ui.quiz.MathQuizPanelUI;
	import ui.quiz.MathQuizSettingPanelUI;
	import ui.quiz.QuizResultPanelUI;
	
	public class ViewManager
	{
		private static var _instance:ViewManager;
		
		private var viewContainer:Sprite;
		
		private var openViewList:Object;
		
		private var topmenu:TopMenuUI;

		public static const VIEW_POPUPDIALOG:String = "VIEW_POPUPDIALOG";//确认框
		
		public static const VIEW_LOGIN_PANEL:String = "VIEW_LOGIN_PANEL";//登录界面
		
		public static const VIEW_REGISTER_PANEL:String = "VIEW_REGISTER_PANEL";//注册界面
		
		
		public static const VIEW_MAIN_PANEL:String = "VIEW_MAIN_PANEL";//主面板

		public static const VIEW_CHOOSE_CLASSLEVEL_PANEL:String = "VIEW_CHOOSE_CLASSLEVEL_PANEL";//选择年级

		public static const VIEW_CHOOSE_ENGLISH_LESSON_PANEL:String = "VIEW_CHOOSE_ENGLISH_LESSON_PANEL";//选择kech
		public static const VIEW_CHOOSE_CHINESE_LESSON_PANEL:String = "VIEW_CHOOSE_CHINESE_LESSON_PANEL";//选择kech

		public static const VIEW_LEARN_WORD_PANEL:String = "VIEW_LEARN_WORD_PANEL";//学习英语
		
		public static const VIEW_MATHQUIZ_SETTING_PANEL:String = "VIEW_MATHQUIZ_SETTING_PANEL";//

		public static const VIEW_MATHQUIZ_PANEL:String = "VIEW_MATHQUIZ_PANEL";//数字考试界面

		public static const VIEW_MATHQUIZ_RESULT_PANEL:String = "VIEW_MATHQUIZ_RESULT_PANEL";//数字结束界面

		public static const VIEW_CHOOSE_RECITE_DATE_PANEL:String = "VIEW_CHOOSE_RECITE_DATE_PANEL";//选择背诵日期
		public static const VIEW_ADD_RECITE_WORD_PANEL:String = "VIEW_ADD_RECITE_WORD_PANEL";//添加背诵单词
		public static const VIEW_BOOK_PAGE_PANEL:String = "VIEW_BOOK_PAGE_PANEL";//书本页
		public static const VIEW_DICTATION_PANEL:String = "VIEW_DICTATION_PANEL";//单词测试默写

		public var viewDict:Object;
		
		private var currentViewName:String = "";
		public static function get instance():ViewManager
		{
			if(_instance == null)
				_instance = new ViewManager();
			return _instance;
		}
		
		public function ViewManager()
		{
			viewContainer = new Sprite();
			
			Laya.stage.addChild(viewContainer);
			
			topmenu = new TopMenuUI();
			
			Laya.stage.addChild(topmenu);
			
			
			var screenHeight:int = window.screen.height;
			var screenWidth:int = window.screen.width;
			
			//if(screenHeight < 1080)
			//	viewContainer.scaleY = screenHeight/1080;
			
			if(screenWidth < 1920)
			{
				//viewContainer.scaleX = screenWidth/1920;
				//viewContainer.x = (1920 - screenWidth)/2;
			}

			
			openViewList = {};
			
			viewDict = new Object();
			
			viewDict[VIEW_POPUPDIALOG] = PopUpDialogUI;
			
			viewDict[VIEW_LOGIN_PANEL] = LoginPanelUI;
			
			viewDict[VIEW_REGISTER_PANEL] = RegisterPanelUI;

			viewDict[VIEW_MAIN_PANEL] = MainPagePanelUI;
			
			viewDict[VIEW_CHOOSE_CLASSLEVEL_PANEL] = ChooseLevelPanelUI;

			viewDict[VIEW_CHOOSE_ENGLISH_LESSON_PANEL] = EnglishBookContentPanelUI;
			viewDict[VIEW_CHOOSE_CHINESE_LESSON_PANEL] = ChineseBookContentPanelUI;

			viewDict[VIEW_LEARN_WORD_PANEL] = LearnWordsPanelUI;
			
			viewDict[VIEW_MATHQUIZ_SETTING_PANEL] = MathQuizSettingPanelUI;
			viewDict[VIEW_MATHQUIZ_PANEL] = MathQuizPanelUI;

			viewDict[VIEW_MATHQUIZ_RESULT_PANEL] = QuizResultPanelUI;
			viewDict[VIEW_CHOOSE_RECITE_DATE_PANEL] = ChooseReciteDatePanelUI;
			viewDict[VIEW_DICTATION_PANEL] = DictationPanelUI;

			viewDict[VIEW_ADD_RECITE_WORD_PANEL] = EnglishWordAddPanelUI;
			viewDict[VIEW_BOOK_PAGE_PANEL] = BookPagePanelUI;

			
			
		}
		
		public static function showAlert(mesg:String):void
		{
			Browser.window.alert(mesg);

		}
		public function openView(viewClass:String,closeOther:Boolean=false,params:Object = null):void
		{
						
			if(closeOther)
			{
				for each(var oldview in openViewList)
				{
					if(oldview != null)
					{
						viewContainer.removeChild(oldview);
						(oldview as View).destroy(true);
					}
					
				}
				openViewList = {};
			}
			
			if(viewDict[viewClass] == null)
				return;
			
			EventCenter.instance.event(EventCenter.OPEN_PANEL_VIEW,viewClass);
			
			if(viewClass == ViewManager.VIEW_MAIN_PANEL || viewClass == VIEW_LOGIN_PANEL)
				topmenu.visible = false;
			else 
				topmenu.visible = true;
			
			currentViewName = viewClass;
			
			if(openViewList[viewClass] != null)
				return;
			
			var view:View = new viewDict[viewClass]();
			view.param = params;
//			var control:BaseViewController = view.getComponent(Script) as BaseViewController;
//			if(control != null)
//				control.viewName = viewClass;
			
			
			viewContainer.addChild(view);
			openViewList[viewClass] = view;
		}
		
		public function closeView(viewClass:String):void
		{
			if(openViewList[viewClass] == null)
			{
				return;
				showAlert("不存在的界面");
			}
			viewContainer.removeChild(openViewList[viewClass]);
			(openViewList[viewClass] as View).destroy(true);
			openViewList[viewClass] = null;
			delete openViewList[viewClass];
			EventCenter.instance.event(EventCenter.COMMON_CLOSE_PANEL_VIEW,viewClass);

		}
		
		public function closeCurrentView():void
		{
			var curview:String = getTopViewName();
			if(curview != VIEW_MAIN_PANEL)
			{
				closeView(curview);
				curview = getTopViewName();
				if(curview == VIEW_MAIN_PANEL)
					topmenu.visible = false;
			}			
					
			
		}
		
		private function getTopViewName():String
		{
			var curview:View = viewContainer.getChildAt(viewContainer.numChildren - 1) as View;

			var viewname:String = "";
			for(var viewclass in openViewList)
			{
				if(openViewList[viewclass] == curview)
				{
					viewname = viewclass;
					break;
				}
			}
			
			return viewname;
		}
	}
}